package com.dapeis.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;

import com.ag.core.exception.SysTipsException;
import com.ag.core.utils.AgAssert;
import com.ag.dao.DBService;
import com.ag.dao.IDao;
import com.dapeis.core.utils.StylistConstants;
import com.dapeis.dao.ICommentExtMapper;
import com.dapeis.dao.ICommentMapper;
import com.dapeis.domain.Comment;

/**
 * @author 作者 E-mail: hzyuanyong@126.com
 * @version 创建时间：2017年5月11日 下午9:01:36 类说明
 */
@Service
public class CommentService extends DBService<Comment> {

    @Resource
    ICommentMapper mapper;
    @Resource
    ICommentExtMapper commentExtMapper;
    
    /**
     * 订单评论
     * 
     * @param userId
     * @param orderId
     * @param requiremntId
     * @param score
     * @param content
     * @return
     */
    public boolean addComment(Integer userId, Long orderId,
	    Integer requiremntId, String score, String content) {
	Comment commentDB = getCommentOrderId(orderId);
	AgAssert.isNull(commentDB, SysTipsException.class, "该订单的评论已经存在，无法再次评价.");
	Comment comment = new Comment();
	comment.buildComment(orderId, userId, requiremntId, score, content);
	return insertSelective(comment) > 0;
    }

    /**
     * 方案评论
     * 
     * @param userId
     * @param planId
     * @param content
     * @return
     */
    public Comment addCommentPlan(Integer userId, Integer planId, String content) {
	Comment comment = new Comment();
	comment.buildCommentPlan(userId, content, planId);
	if (insertSelective(comment) > 0)
	    return comment;
	return null;
    }

    public Comment getCommentOrderId(Long orderId) {
	Example example = example();
	example.createCriteria().andEqualTo("orderId", orderId);
	return selectOneByExample(example);
    }

    @Override
    protected IDao<Comment> getIDao() {
	return mapper;
    }

    public ICommentExtMapper getCommentExtMapper() {
        return commentExtMapper;
    }
    
}
